package com.jzqf.harmonydemo.library.database;

import ohos.aafwk.ability.AbilitySlice;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;

/**
 * 数据管理类
 *
 * @author LiuWeiHao
 * 2021/5/10 9:22
 */
public class DatabaseManager {

    private static OrmContext ormContext;
    private static AbilitySlice abilitySlice;

    public static void setAbilitySlice(AbilitySlice abilitySlice) {
        DatabaseManager.abilitySlice = abilitySlice;
    }

    public static OrmContext getOrmContext() {
        if (ormContext == null) {
            synchronized (DatabaseManager.class) {
                //context必须传入abilitySlice类型,否则报错
                DatabaseHelper databaseHelper = new DatabaseHelper(abilitySlice);
                ormContext = databaseHelper.getOrmContext("Harmony", "Harmony.db",
                        HarmonyDatabase.class);
            }
        }
        return ormContext;
    }
}
